options ps=MAX ls=MAX nodate nonumber nocenter;
*ods listing;

* CHANGING THE WORK DIRECTORY; 
libname user "C:\Users\Public\Documents\Sas_work_dir";
libname rec "C:\Users\Public\Documents\Results\Segreg_panel_wgt";
libname test "C:\Users\Public\Documents\Test_data";

libname pan "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_Panel tous salari�s_2018";
*libname test "P:\Test_data";
libname po1994 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1994";
libname po1995 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1995";
libname po1996 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1996";
libname po1997 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1997";
libname po1998 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1998";
libname po1999 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1999";
libname po2000 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2000";
libname po2001 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2001";
libname po2002 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2002";
libname po2003 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2003";
libname po2004 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2004";
libname po2005 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2005";
libname po2006 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2006";
libname po2007 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2007";
libname po2008 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2008";
libname po2009 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2009";
libname po2010 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2010";
libname po2011 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2011";
libname po2012 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2012";
libname po2013 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2013";
libname po2014 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2014\R�gions";
libname po2015 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2015\R�gions";
libname po2016 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2016\R�gions";
libname po2017 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2017\R�gions";
libname po2018 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2018";
libname po2019 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2019";

libname pi2012 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2012\Ile de France";
libname pi2013 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2013\Ile de France";
libname pi2014 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2014\D�partements";
libname pi2015 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2015\D�partements";
libname pi2016 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2016\D�partements";
libname pi2017 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2017\D�partements";


*BE: Mining, Manufacturing and Energy;
* F: Construction;
* A&G: A:Agriculture & G:Trade;
* H: Transportation;
* I: Accomodation;
* J & : J. Info. & Comm & R: Leisure;
* K: Finance;
* LM: Real Estate and professional services;
* N: Support services;
* OPQ: State, teaching and health; 
* STU: Other;



proc format; 

	value $naf03_naf08_
	"011A"="0111Z"
	"011C"="0111Z"
	"011D"="0113Z"
	"011F"="0122Z"
	"011G"="0121Z"
	"012A"="0141Z"
	"012C"="0143Z"
	"012E"="0146Z"
	"012G"="0147Z"
	"012J"="0144Z"
	"013Z"="0150Z"
	"014A"="0161Z"
	"014B"="8130Z"
	"014D"="0162Z"
	"015Z"="0170Z"
	"020A"="0129Z"
	"020B"="0220Z"
	"020D"="0240Z"
	"050A"="0311Z"
	"050C"="0321Z"
	"101Z"="0510Z"
	"102Z"="0520Z"
	"103Z"="0892Z"
	"111Z"="0610Z"
	"112Z"="0910Z"
	"120Z"="0721Z"
	"131Z"="0710Z"
	"132Z"="0729Z"
	"141A"="0811Z"
	"141C"="0811Z"
	"141E"="0811Z"
	"142A"="0812Z"
	"142C"="0812Z"
	"143Z"="0891Z"
	"144Z"="0893Z"
	"145Z"="0899Z"
	"151A"="0149Z"
	"151C"="1011Z"
	"151E"="1013A"
	"151F"="1013B"
	"152Z"="1020Z"
	"153A"="1031Z"
	"153C"="1032Z"
	"153E"="1039A"
	"153F"="1039B"
	"154A"="1041A"
	"154C"="1041B"
	"154E"="1042Z"
	"155A"="1051A"
	"155B"="1051B"
	"155C"="1051C"
	"155D"="1051D"
	"155F"="1052Z"
	"156A"="1061A"
	"156B"="1061B"
	"156D"="1062Z"
	"157A"="1091Z"
	"157C"="1092Z"
	"158A"="1071A"
	"158B"="1071B"
	"158C"="1071C"
	"158D"="1071D"
	"158F"="1072Z"
	"158H"="1081Z"
	"158K"="1082Z"
	"158M"="1073Z"
	"158P"="1083Z"
	"158R"="1084Z"
	"158T"="1086Z"
	"158V"="1085Z"
	"159A"="1101Z"
	"159B"="1101Z"
	"159D"="1101Z"
	"159F"="1102A"
	"159G"="1102B"
	"159J"="1103Z"
	"159L"="1104Z"
	"159N"="1105Z"
	"159Q"="1106Z"
	"159S"="1107A"
	"159T"="1107B"
	"160Z"="1200Z"
	"171A"="1310Z"
	"171C"="1310Z"
	"171E"="1310Z"
	"171F"="1310Z"
	"171H"="1310Z"
	"171K"="1310Z"
	"171M"="1310Z"
	"171P"="1310Z"
	"172A"="1320Z"
	"172C"="1320Z"
	"172E"="1320Z"
	"172G"="1320Z"
	"172J"="1320Z"
	"173Z"="1330Z"
	"174A"="1392Z"
	"174B"="1392Z"
	"174C"="1392Z"
	"175A"="1393Z"
	"175C"="1394Z"
	"175E"="1395Z"
	"175G"="1396Z"
	"176Z"="1391Z"
	"177A"="1419Z"
	"177C"="1439Z"
	"181Z"="1411Z"
	"182A"="1412Z"
	"182C"="1413Z"
	"182D"="1413Z"
	"182E"="1413Z"
	"182G"="1414Z"
	"182J"="1419Z"
	"183Z"="1320Z"
	"191Z"="1511Z"
	"192Z"="1512Z"
	"193Z"="1520Z"
	"201A"="1610A"
	"201B"="1610B"
	"202Z"="1621Z"
	"203Z"="1622Z"
	"204Z"="1624Z"
	"205A"="1629Z"
	"205C"="1629Z"
	"211A"="1711Z"
	"211C"="1712Z"
	"212A"="1721A"
	"212B"="1721B"
	"212C"="1721C"
	"212E"="1722Z"
	"212G"="1723Z"
	"212J"="1724Z"
	"212L"="1729Z"
	"221A"="3299Z"
	"221C"="5813Z"
	"221E"="5814Z"
	"221G"="5920Z"
	"221J"="5819Z"
	"222A"="1811Z"
	"222C"="1723Z"
	"222E"="1814Z"
	"222G"="1813Z"
	"222J"="1813Z"
	"223A"="1820Z"
	"223C"="1820Z"
	"223E"="1820Z"
	"231Z"="1910Z"
	"232Z"="1920Z"
	"233Z"="2013A"
	"241A"="2011Z"
	"241C"="2012Z"
	"241E"="2013B"
	"241G"="1910Z"
	"241J"="0891Z"
	"241L"="2016Z"
	"241N"="2017Z"
	"242Z"="2020Z"
	"243Z"="2030Z"
	"244A"="2110Z"
	"244C"="2120Z"
	"244D"="2120Z"
	"245A"="2041Z"
	"245C"="2042Z"
	"246A"="2051Z"
	"246C"="2052Z"
	"246E"="2053Z"
	"246G"="2059Z"
	"246J"="2680Z"
	"246L"="2059Z"
	"247Z"="2060Z"
	"251A"="2211Z"
	"251C"="2211Z"
	"251E"="2219Z"
	"252A"="2221Z"
	"252C"="2222Z"
	"252E"="2223Z"
	"252G"="2229A"
	"252H"="2229A"
	"261A"="2311Z"
	"261C"="2312Z"
	"261E"="2313Z"
	"261G"="2314Z"
	"261J"="2319Z"
	"261K"="2319Z"
	"262A"="2341Z"
	"262C"="2342Z"
	"262E"="2343Z"
	"262G"="2344Z"
	"262J"="2349Z"
	"262L"="2320Z"
	"263Z"="2331Z"
	"264A"="2332Z"
	"264B"="2332Z"
	"264C"="2332Z"
	"265A"="2351Z"
	"265C"="2352Z"
	"265E"="2352Z"
	"266A"="2361Z"
	"266C"="2362Z"
	"266E"="2363Z"
	"266G"="2364Z"
	"266J"="2365Z"
	"266L"="2369Z"
	"267Z"="2370Z"
	"268A"="2391Z"
	"268C"="2399Z"
	"271Y"="2410Z"
	"272A"="2451Z"
	"272C"="2420Z"
	"273A"="2431Z"
	"273C"="2432Z"
	"273E"="2433Z"
	"273G"="2434Z"
	"274A"="2441Z"
	"274C"="2442Z"
	"274D"="2442Z"
	"274F"="2443Z"
	"274G"="2443Z"
	"274J"="2444Z"
	"274K"="2444Z"
	"274M"="2445Z"
	"275A"="2451Z"
	"275C"="2452Z"
	"275E"="2453Z"
	"275G"="2454Z"
	"281A"="2433Z"
	"281C"="2512Z"
	"282C"="2529Z"
	"282D"="2521Z"
	"283A"="2530Z"
	"283B"="2530Z"
	"283C"="3311Z"
	"284A"="2550A"
	"284B"="2550B"
	"284C"="2550A"
	"285A"="2561Z"
	"285C"="2562A"
	"285D"="2562B"
	"286A"="2571Z"
	"286C"="2573B"
	"286D"="2573B"
	"286F"="2572Z"
	"287A"="2591Z"
	"287C"="2592Z"
	"287E"="2593Z"
	"287G"="2594Z"
	"287H"="2593Z"
	"287J"="2593Z"
	"287L"="2599A"
	"287N"="2599B"
	"287Q"="2571Z"
	"291A"="2811Z"
	"291B"="2812Z"
	"291D"="2812Z"
	"291E"="2813Z"
	"291F"="2812Z"
	"291H"="2815Z"
	"291J"="2815Z"
	"292A"="2821Z"
	"292C"="2822Z"
	"292D"="2822Z"
	"292F"="2825Z"
	"292H"="2829A"
	"292J"="2829A"
	"292L"="2829B"
	"292M"="2829B"
	"293A"="2830Z"
	"293C"="3312Z"
	"293D"="2830Z"
	"294A"="2841Z"
	"294B"="2849Z"
	"294C"="2824Z"
	"294D"="2790Z"
	"294E"="2849Z"
	"295A"="2891Z"
	"295B"="2892Z"
	"295D"="2892Z"
	"295E"="2893Z"
	"295G"="2894Z"
	"295J"="2895Z"
	"295L"="2894Z"
	"295M"="2896Z"
	"295N"="2573A"
	"295Q"="2899B"
	"295R"="2894Z"
	"296A"="2540Z"
	"296B"="2540Z"
	"297A"="2751Z"
	"297C"="2752Z"
	"300A"="2620Z"
	"300C"="2620Z"
	"311A"="2611Z"
	"311B"="2611Z"
	"311C"="3313Z"
	"312A"="2611Z"
	"312B"="2611Z"
	"313Z"="2611Z"
	"314Z"="2720Z"
	"315A"="2740Z"
	"315B"="2740Z"
	"315C"="2740Z"
	"316A"="2740Z"
	"316C"="2599B"
	"316D"="2344Z"
	"321A"="2611Z"
	"321C"="2611Z"
	"321D"="2612Z"
	"322A"="2630Z"
	"322B"="2630Z"
	"323Z"="2620Z"
	"331A"="2660Z"
	"331B"="2660Z"
	"332A"="2651A"
	"332B"="2651B"
	"333Z"="3320C"
	"334A"="3250B"
	"334B"="2670Z"
	"335Z"="2652Z"
	"341Z"="2892Z"
	"342A"="2920Z"
	"342B"="2920Z"
	"343Z"="2811Z"
	"351A"="3011Z"
	"351B"="3011Z"
	"351C"="3011Z"
	"351E"="3012Z"
	"352Z"="3020Z"
	"353A"="2899B"
	"353B"="3030Z"
	"353C"="3030Z"
	"354A"="3091Z"
	"354C"="3092Z"
	"354E"="3092Z"
	"355Z"="2822Z"
	"361A"="2932Z"
	"361C"="2823Z"
	"361E"="3102Z"
	"361G"="3109B"
	"361H"="3109B"
	"361J"="3109B"
	"361K"="3109B"
	"361M"="3103Z"
	"362A"="3211Z"
	"362C"="3212Z"
	"363Z"="3220Z"
	"364Z"="3230Z"
	"365Z"="2640Z"
	"366A"="3213Z"
	"366C"="3291Z"
	"366E"="1399Z"
	"371Z"="3831Z"
	"372Z"="3831Z"
	"401A"="3511Z"
	"401C"="3512Z"
	"401E"="3513Z"
	"402A"="3521Z"
	"402C"="3522Z"
	"403Z"="3530Z"
	"410Z"="3600Z"
	"451A"="4311Z"
	"451B"="4312B"
	"451D"="4313Z"
	"452A"="4120A"
	"452B"="4120B"
	"452C"="4120B"
	"452D"="4212Z"
	"452E"="4221Z"
	"452F"="4222Z"
	"452J"="4391B"
	"452K"="4399A"
	"452L"="4391A"
	"452N"="4212Z"
	"452P"="4120B"
	"452R"="4221Z"
	"452T"="4399B"
	"452U"="3900Z"
	"452V"="4399C"
	"453A"="4321A"
	"453C"="4329A"
	"453E"="4322A"
	"453F"="4322B"
	"453H"="4321B"
	"454A"="4331Z"
	"454C"="4332A"
	"454D"="4332B"
	"454F"="4333Z"
	"454H"="4334Z"
	"454J"="4334Z"
	"454L"="4332C"
	"454M"="4339Z"
	"455Z"="4399E"
	"501Z"="4511Z"
	"502Z"="4520A"
	"503A"="4531Z"
	"503B"="4532Z"
	"504Z"="4540Z"
	"505Z"="4730Z"
	"511A"="4611Z"
	"511C"="4612B"
	"511E"="4613Z"
	"511G"="4614Z"
	"511J"="4615Z"
	"511L"="4616Z"
	"511N"="4617B"
	"511P"="4617A"
	"511R"="4618Z"
	"511T"="4619B"
	"511U"="4612A"
	"512A"="4621Z"
	"512C"="4622Z"
	"512E"="4623Z"
	"512G"="4624Z"
	"512J"="4621Z"
	"513A"="1039A"
	"513C"="4632A"
	"513D"="4632B"
	"513E"="4632C"
	"513G"="4633Z"
	"513J"="1101Z"
	"513L"="4635Z"
	"513N"="4636Z"
	"513Q"="4637Z"
	"513S"="4638A"
	"513T"="4638B"
	"513V"="4639A"
	"513W"="4639B"
	"514A"="4641Z"
	"514C"="4642Z"
	"514D"="4642Z"
	"514F"="4643Z"
	"514H"="4644Z"
	"514J"="4644Z"
	"514L"="4645Z"
	"514N"="4646Z"
	"514Q"="4649Z"
	"514R"="4649Z"
	"514S"="4643Z"
	"515A"="4671Z"
	"515C"="4672Z"
	"515E"="4673A"
	"515F"="4673A"
	"515H"="4674A"
	"515J"="4674B"
	"515L"="4675Z"
	"515N"="4676Z"
	"515Q"="4677Z"
	"518A"="4662Z"
	"518C"="4663Z"
	"518E"="4664Z"
	"518G"="4651Z"
	"518H"="4665Z"
	"518J"="4652Z"
	"518L"="4669A"
	"518M"="4669B"
	"518N"="4669C"
	"518P"="4661Z"
	"519A"="4690Z"
	"519B"="4690Z"
	"521A"="4711A"
	"521B"="4711B"
	"521C"="4711C"
	"521D"="4711D"
	"521E"="4711E"
	"521F"="4711F"
	"521H"="4719A"
	"521J"="4719B"
	"522A"="4721Z"
	"522C"="4722Z"
	"522E"="4723Z"
	"522G"="4724Z"
	"522J"="4725Z"
	"522L"="4726Z"
	"522N"="4729Z"
	"522P"="4721Z"
	"523A"="4773Z"
	"523C"="4774Z"
	"523E"="4775Z"
	"524A"="4751Z"
	"524C"="4771Z"
	"524E"="4772A"
	"524F"="4772B"
	"524H"="4759A"
	"524J"="4753Z"
	"524L"="4743Z"
	"524N"="4752A"
	"524P"="4752B"
	"524R"="4761Z"
	"524T"="4778A"
	"524U"="4753Z"
	"524V"="4777Z"
	"524W"="4764Z"
	"524X"="4776Z"
	"524Y"="4778B"
	"524Z"="4741Z"
	"525Z"="4779Z"
	"526A"="4791A"
	"526B"="4791B"
	"526D"="4781Z"
	"526E"="4782Z"
	"526G"="4799A"
	"526H"="4799B"
	"527A"="9523Z"
	"527C"="9521Z"
	"527D"="9522Z"
	"527F"="9525Z"
	"527H"="9512Z"
	"551A"="5510Z"
	"551C"="5510Z"
	"551E"="5510Z"
	"552A"="5520Z"
	"552C"="5530Z"
	"552E"="5510Z"
	"552F"="5590Z"
	"553A"="5610A"
	"553B"="5610C"
	"554A"="5630Z"
	"554B"="5630Z"
	"554C"="5630Z"
	"555A"="5629B"
	"555C"="5629A"
	"555D"="5621Z"
	"601Z"="4910Z"
	"602A"="4931Z"
	"602B"="4939A"
	"602C"="4931Z"
	"602E"="4932Z"
	"602G"="4939B"
	"602L"="4941B"
	"602M"="4941A"
	"602N"="4942Z"
	"602P"="4941C"
	"603Z"="4950Z"
	"611A"="5010Z"
	"611B"="5010Z"
	"612Z"="5030Z"
	"621Z"="5110Z"
	"622Z"="5110Z"
	"623Z"="5122Z"
	"631A"="5224A"
	"631B"="5224B"
	"631D"="5210A"
	"631E"="5210B"
	"632A"="4932Z"
	"632C"="5222Z"
	"632E"="5223Z"
	"633Z"="7911Z"
	"634A"="5229A"
	"634B"="5229B"
	"634C"="5229B"
	"641A"="5310Z"
	"641C"="5320Z"
	"642C"="6110Z"
	"642D"="6110Z"
	"651A"="6411Z"
	"651C"="6419Z"
	"651D"="6419Z"
	"651E"="6419Z"
	"651F"="6419Z"
	"652A"="6491Z"
	"652C"="6492Z"
	"652E"="6420Z"
	"652F"="6499Z"
	"660A"="6511Z"
	"660C"="6520Z"
	"660E"="6512Z"
	"660F"="6520Z"
	"660G"="6512Z"
	"671A"="6611Z"
	"671C"="6612Z"
	"671E"="6612Z"
	"672Z"="6621Z"
	"701A"="4110A"
	"701B"="4110B"
	"701C"="4110C"
	"701D"="4110D"
	"701F"="6810Z"
	"702A"="6820A"
	"702B"="6820B"
	"702C"="6820B"
	"703A"="6831Z"
	"703C"="6832A"
	"703D"="6832A"
	"703E"="6619A"
	"711A"="7711A"
	"711B"="7711B"
	"712A"="7712Z"
	"712C"="7734Z"
	"712E"="7735Z"
	"713A"="7731Z"
	"713C"="7732Z"
	"713E"="7733Z"
	"713G"="7739Z"
	"714A"="7729Z"
	"714B"="7721Z"
	"721Z"="6202A"
	"722A"="5821Z"
	"722C"="6201Z"
	"723Z"="6203Z"
	"724Z"="5811Z"
	"725Z"="3312Z"
	"726Z"="6209Z"
	"731Z"="7211Z"
	"732Z"="7220Z"
	"741A"="6910Z"
	"741C"="6920Z"
	"741E"="7320Z"
	"741G"="0240Z"
	"741J"="6420Z"
	"742A"="7111Z"
	"742B"="7112A"
	"742C"="7112B"
	"743A"="7120A"
	"743B"="7120B"
	"744A"="7311Z"
	"744B"="7311Z"
	"745A"="7810Z"
	"745B"="7820Z"
	"746Z"="7490B"
	"747Z"="8121Z"
	"748A"="7420Z"
	"748B"="7420Z"
	"748D"="8292Z"
	"748F"="7430Z"
	"748G"="8219Z"
	"748H"="8220Z"
	"748J"="8230Z"
	"748K"="4779Z"
	"751A"="8411Z"
	"751C"="8412Z"
	"751E"="8413Z"
	"751G"="8110Z"
	"752A"="8421Z"
	"752C"="8422Z"
	"752E"="8423Z"
	"752G"="8424Z"
	"752J"="8425Z"
	"753A"="8430A"
	"753B"="8430B"
	"753C"="8430C"
	"801Z"="8510Z"
	"802A"="8531Z"
	"802C"="8532Z"
	"803Z"="8541Z"
	"804A"="8532Z"
	"804C"="8532Z"
	"804D"="8552Z"
	"851A"="8610Z"
	"851C"="8621Z"
	"851E"="8623Z"
	"851G"="8690D"
	"851H"="8690E"
	"851J"="8690A"
	"851K"="8690B"
	"851L"="8690C"
	"852Z"="7500Z"
	"853A"="8710B"
	"853B"="8710B"
	"853C"="8710C"
	"853D"="8710A"
	"853E"="8720A"
	"853G"="8891A"
	"853H"="8810B"
	"853J"="8810A"
	"853K"="8899A"
	"900A"="3700Z"
	"900B"="3811Z"
	"900E"="3812Z"
	"900G"="3811Z"
	"911A"="9411Z"
	"911C"="9412Z"
	"912Z"="9420Z"
	"913A"="9491Z"
	"913C"="9492Z"
	"913E"="9499Z"
	"921A"="5911A"
	"921B"="5911B"
	"921C"="5911C"
	"921D"="5911C"
	"921F"="5913A"
	"921G"="5913B"
	"921J"="5914Z"
	"922A"="5920Z"
	"922B"="5911A"
	"922D"="6020A"
	"922E"="6020B"
	"922F"="6110Z"
	"923A"="9001Z"
	"923B"="9002Z"
	"923D"="7990Z"
	"923F"="9321Z"
	"923K"="7990Z"
	"924Z"="6391Z"
	"925A"="9101Z"
	"925C"="9102Z"
	"925E"="9104Z"
	"926A"="9311Z"
	"926C"="7990Z"
	"927A"="9200Z"
	"927C"="0162Z"
	"930A"="9601A"
	"930B"="9601B"
	"930D"="9602A"
	"930E"="9602B"
	"930G"="9603Z"
	"930H"="9603Z"
	"930K"="9604Z"
	"930L"="9313Z"
	"930N"="8551Z"
	"950Z"="9700Z"
	"960Z"="9810Z"
	"970Z"="9820Z"
	"990Z"="9900Z"
	"0320"="0321Z"
"2202"="2830Z"
"4107"="1103Z"
"5510"="8130Z"
"5560"="4399C"
"5571"="4332B"
"5701"="4611Z"
"6007"="4611Z"
"6211"="4711B"
"6241"="4721Z"
"6411"="4771Z"
"6414"="4751Z"
"6425"="4719B"
"6502"="4730Z"
"6702"="5629A"
"7708"="6910Z"
"7712"="4339Z"
"7713"="7820Z"
"7715"="9412Z"
"8111"="6820A"
"8121"="6820B"
"8206"="8553Z"
"8410"="8621Z"
"8503"="8710C"
"8606"="9001Z"
"8608"="9002Z"
"8610"="8531Z"
"8804"="6512Z"
"9011"="8411Z"
"9012"="8413Z"
"9212"="8520Z"
"9214"="8531Z"
"9616"="9329Z"
"9625"="9312Z"
"9722"="9491Z"
"9723"="9499Z"
"271Z"="2410Z"
"273J"="2445Z"
"282A"="2529Z"
"282B"="2529Z"
"287M"="2572Z"
"287P"="2599B"
"291C"="2813Z"
"292K"="2829B"
"295C"="2892Z"
"295P"="2899B"
"321B"="2611Z"
"401Z"="2511Z"
"402Z"="3521Z"
"516A"="4662Z"
"516C"="4663Z"
"516E"="4664Z"
"516G"="4651Z"
"516J"="4669A"
"516K"="4669B"
"516L"="4669C"
"516N"="4661Z"
"517Z"="4690Z"
"551D"="5590Z"
"642A"="6110Z"
"642B"="6190Z"
"711Z"="7711A"
"722Z"="5829C"
"900C"="3822Z"
"922C"="6020A"
"923H"="9329Z"
"923J"="9001Z"
	;

	value $div03_sect03_
	"01"="A"
	"02"="A"
	"05"="B"
	"10"="C"
	"11"="C"
	"12"="C"
	"13"="C"
	"14"="C"
	"15"="D"
	"16"="D"
	"17"="D"
	"18"="D"
	"19"="D"
	"20"="D"
	"21"="D"
	"22"="D"
	"23"="D"
	"24"="D"
	"25"="D"
	"26"="D"
	"27"="D"
	"28"="D"
	"29"="D"
	"30"="D"
	"31"="D"
	"32"="D"
	"33"="D"
	"34"="D"
	"35"="D"
	"36"="D"
	"37"="D"
	"40"="E"
	"41"="E"
	"45"="F"
	"50"="G"
	"51"="G"
	"52"="G"
	"55"="H"
	"60"="I"
	"61"="I"
	"62"="I"
	"63"="I"
	"64"="I"
	"65"="J"
	"66"="J"
	"67"="J"
	"70"="K"
	"71"="K"
	"72"="K"
	"73"="K"
	"74"="K"
	"75"="L"
	"80"="M"
	"85"="N"
	"90"="O"
	"91"="O"
	"92"="O"
	"93"="O"
	"95"="P"
	"96"="P"
	"97"="P"
	"99"="Q"
	;

	value $sect03_ag
	"A"-"D"="AD"
	"E"-"F"="EF"
	"G"-"H"="GH"
	"I"="I"
	"J"="J"
	"K"="K"
	"L"-"M"="LM"
	"N"="N"
	"O"-"Q"="OPQ"
	OTHER="OPQ";
	;


value $ind08_sectagg
		"05"-"39"="BE"
		"41"-"43"="F"
		"01"-"03","45"-"47"="A&G"
		"49"-"53"="H"
		"55"-"56"="I"
		"58"-"63","90"-"93"="J&R"
		"64"-"66"="K"
		"68"-"75"="LM"
		"77"-"82"="N"
		"84"-"88"="OPQ"
		"","94"-"99"="STU"
		OTHER="STU";

	value $ZEAT
	 "75","77","78","91"-"95"="1.IledeFrance"
	"08","10","51","52","02","60","80","27","76","18","28","36","37","41","45","14","50","61","21","58","71","89"="2.BassinParisien"
	"59","62"="3.Nord"
	"54","55","57","88","67","68","25","39","70","90"="4.Est"
	"44","49","53","72","85","22","29","35","56","16","17","79","86"="5.Ouest"
	"24","33","40","47","64","09","12","31","32","46","65","81","82","19","23","87"="7.SudOuest"
	"01","07","26","38","42","69","73","74","03","15","43","63"="8.CentreEst"
	"11","30","34","48","66","04","05","06","13","83","84","20","2A","2B","2a","2b"="9.M�diterrrann�e";



VALUE rk
0.00-0.25="1.F0025"
0.25-0.75="2.F2575"
0.75-0.90="3.F7590"
0.90-0.99="4.F9099" 
0.99-1.001="5.F9910";

VALUE decile
0.0-0.1="d0"
0.1-0.2="d1"
0.2-0.3="d2"
0.3-0.4="d3"
0.4-0.5="d4"
0.5-0.6="d5"
0.6-0.7="d6"
0.7-0.8="d7"
0.8-0.9="d8"
0.9-1.001="d9";

VALUE age
00-30="age15-30"
31-40="age31-40"
41-55="age41-55"
56-199="age>=56" ;

value $orgsizeb
"00","01","02","03"="size_0-19"
"04"="size_20-49"
"05"="size_50-99"
"06"="size_100-199"
"07"="size_200-499" 
"08"="size_500-999" 
"09","10","11"="size_>1000" 
;

value orgsize
0="orgsize0"
1="orgsize1"
2-20="orgsize2"
21-50="orgsize20"
51-100="orgsize50"
101-200="orgsize100"
201-500="orgsize200"
501-1000="orgsize500" 
1001-2500="orgsize1000" 
2501-5000="orgsize2500" 
5001-10000="orgsize5000" 
10001-10000000="orgsize10000_plus"
;

run;


%macro b_g(year);
	%if &year<2016 %then %do; 
		%let IND_ETRANGER= IND_ETRANGER;
		%let rename= RENAME=(IND_ETRANGER=ETRANGER PCS=CS) ; 
	%end;
	%else %do; 
		%let IND_ETRANGER= ;
		%let rename= RENAME=(PCS=CS) ; 
	%end;
	%let keep= COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE &IND_ETRANGER PCS NIC REGT DOMEMPL_EMPL NBHEUR DUREE DEPT  ;
	%let where=DOMEMPL_EMPL not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I");
	
	data b&year; 
		set 
		pi&year..post75 (&rename KEEP=&keep where=(DEPT="75" AND &where))
		pi&year..post77 (&rename KEEP=&keep where=(DEPT="77" AND &where))
		pi&year..post78 (&rename KEEP=&keep where=(DEPT="78" AND &where))
		pi&year..post91 (&rename KEEP=&keep where=(DEPT="91" AND &where))
		pi&year..post92 (&rename KEEP=&keep where=(DEPT="92" AND &where))
		pi&year..post93 (&rename KEEP=&keep where=(DEPT="93" AND &where))
		pi&year..post94 (&rename KEEP=&keep where=(DEPT="94" AND &where))
		pi&year..post95 (&rename KEEP=&keep where=(DEPT="95" AND &where))

		po&year..post24 (&rename KEEP=&keep WHERE=(REGT="24" AND &where)) 
		po&year..post27 (&rename KEEP=&keep WHERE=(REGT="27" AND &where)) 
		po&year..post28 (&rename KEEP=&keep WHERE=(REGT="28" AND &where)) 
		po&year..post32 (&rename KEEP=&keep WHERE=(REGT="32" AND &where)) 
		po&year..post44 (&rename KEEP=&keep WHERE=(REGT="44" AND &where)) 
		po&year..post52 (&rename KEEP=&keep WHERE=(REGT="52" AND &where)) 
		po&year..post53 (&rename KEEP=&keep WHERE=(REGT="53" AND &where)) 
		po&year..post75 (&rename KEEP=&keep WHERE=(REGT="75" AND &where)) 
		po&year..post76 (&rename KEEP=&keep WHERE=(REGT="76" AND &where)) 
		po&year..post84 (&rename KEEP=&keep WHERE=(REGT="84" AND &where)) 
		po&year..post93 (&rename KEEP=&keep WHERE=(REGT="93" AND &where))
		po&year..post94 (&rename KEEP=&keep WHERE=(REGT="94" AND &where))

		;
			year=&year;
		%common;		 
		drop TYP_EMPLOI DOMEMPL_EMPL;
	run;
%mend;
%macro common;
if siren NOT in ("","0","000000000","00000000");

if year<=2001 then siret=compress(""!!compress(substr(siren,2,9)!!substr(nic,2,5))!!"");
else siret=compress(""!!compress(siren!!nic)!!"");

weight=1;
myid=_N_;

*THRESHOLD DEFINITION THANKS TO MINIMUM WAGE ;
if round(year,1)=1976 then smic=8.94; 
else if round(year,1)=1977 then smic=10.06; 
else if round(year,1)=1978 then smic=11.31; 
else if round(year,1)=1979 then smic=12.93; 
else if round(year,1)=1980 then smic=14.79; 
else if round(year,1)=1980 then smic=14.79; 
else if round(year,1)=1981 then smic=17.76;
else if round(year,1)=1982 then smic=20.29;
else if round(year,1)=1983 then smic=22.33;
else if round(year,1)=1984 then smic=24.36;
else if round(year,1)=1985 then smic=26.04;
else if round(year,1)=1986 then smic=26.92;
else if round(year,1)=1987 then smic=27.84;
else if round(year,1)=1988 then smic=28.76;
else if round(year,1)=1989 then smic=29.91;
else if round(year,1)=1990 then smic=31.94;
else if round(year,1)=1991 then smic=32.66;
else if round(year,1)=1992 then smic=34.06;
else if round(year,1)=1993 then smic=34.83;
else if round(year,1)=1994 then smic=35.56;
else if round(year,1)=1995 then smic=36.98;
else if round(year,1)=1996 then smic=37.91;
else if round(year,1)=1997 then smic=39.43;
else if round(year,1)=1998 then smic=40.22;
else if round(year,1)=1999 then smic=40.72;
else if round(year,1)=2000 then smic=42.02/6.55957;
else if round(year,1)=2001 then smic=43.72/6.55957;
else if round(year,1)=2002 then smic=6.83;
else if round(year,1)=2003 then smic=7.19;
else if round(year,1)=2004 then smic=7.61;
else if round(year,1)=2005 then smic=8.03;
else if round(year,1)=2006 then smic=8.27;
else if round(year,1)=2007 then smic=8.44;
else if round(year,1)=2008 then smic=8.63;
else if round(year,1)=2009 then smic=8.82;
else if round(year,1)=2010 then smic=8.86;
else if round(year,1)=2011 then smic=9.19;
else if round(year,1)=2012 then smic=9.40;
else if round(year,1)=2013 then smic=9.43;
else if round(year,1)=2014 then smic=9.53;
else if round(year,1)=2015 then smic=9.61;
else if round(year,1)=2016 then smic=9.67;
else if round(year,1)=2017 then smic=9.88;
else if round(year,1)=2018 then smic=9.88;
else if round(year,1)=2019 then smic=10.03;
else if round(year,1)=2020 then smic=10.15;
else if round(year,1)=2021 then smic=10.25;

threshold=0.5*smic*1600; * Redefine selection threshold according to your data / 1600 => in France, number of hours a year at 35 hours ;
included=(threshold<=S_BRUT<=smic*1600*1000 and DUREE>359 );
if included=1 then lnwage=log(s_brut);

random=ranuni(15467);

*3 OCCUPATIONS DEFINITION;
Occ1=("21"<=CS<"40")*1;
Occ2=("40"<=CS<"50")*1;
Occ3=("50"<=CS<"70")*1;

if occ1=1 then occ_g=1;
else if occ2=1 then occ_g=2;
else occ_g=3;

*AGE GROUP DEFINITION;
age1=(AGE<=30);
age2=(31<AGE<=40);
age3=(41<AGE<=55);
age4=(55<AGE);

if age1=1 then age_g=1;
else if age2=1 then age_g=2;
else if age3=1 then age_g=3;
else if age4=1 then age_g=4;

*GENDER DEFINITION;
female=(sexe=2);
male=1-female;

*MIGRANT ORIGIN DEFINITION;
if MISSING(DEP_NAISS)=0 then migrant=(DEP_NAISS="99")*1;
nonmigrant=1-migrant;

*SECTORS DEFINITION;
length sector $5.;
if year<2008 then sector=PUT(APET,$naf03_naf08_.); 
else sector=APET; 

if year<2008 then sector_agg=PUT(substr(PUT(sector,$naf03_naf08_.),1,2), $ind08_sectagg.); 
else sector_agg=PUT(substr(sector,1,2), $ind08_sectagg.); 


fin_service=(substr(sector,1,2) in ("64","65","66")); 
manufacturing=("05"<=substr(sector,1,2) <="39"); 


*REGIONAL VARIABLES DEFINITION;
financial_center=(REGT="11");
nuts1=put(substr(COMT,1,2),$ZEAT.);
nuts3=substr(COMT,1,2);


*RANDOM VARIABLE (For sorting);
Random=RANUNI(124567);

*Inclusion/exclusion;
if 0<S_BRUT<300000000;

* Agriculture trees & wood exploitation exclusion (but not fisheries, etc.);
* THIS is partly due to time inconsistencies in French data on agricultural firms (specific social security);
if substr(APET,1,2) not in ("","00","01","02");

rename siren=firm siret=est S_BRUT=wage comt=wtown comr=ltown;

DROP NIC NBHEUR DUREE CPFD SMIC DEP_NAISS 
	 REGT CS SEXE ETRANGER age /*education*/ 
	 APET threshold 
	; 
%mend;

%macro seg2(b);

	%if &b=b1994_1 %then %let year=1993.1;
	%else %if &b=b2002_1 %then %let year=2001.1;
	%else %if &b=b2009_1 %then %let year=2008.1;
	%else %let year=%substr(&b,2,4);

	%let psyear=&year;
	%if &year<1995 %then %let psyear=1995;
	%if &year=2001.1 %then %let psyear=2002;
	%if &year=2008.1 %then %let psyear=2009;

	%let year2=%substr(&psyear,3,2);
	%let unit=est;
			proc sql; 
				create table &b.1 as select *, 
				SUM ((included=1)) as &unit._nb,
				SUM (weight*(included=1)) as &unit._weight
				from &b group by &unit.;
			quit;

			proc datasets LIBRARY=user; 
				DELETE &b;
			RUN;
			

			proc sort data=&b.1 ; 
				by wage
				random; 
			run;

			data  &b.2; set &b.1 (where=(included=1 and &unit._nb>1 and missing(&unit)=0 )) 
									end=last; 
				sumweight + weight ;
				if last then call symput ('SUM', trim(left(put(sumweight, best.))));
			run;

			proc datasets LIBRARY=user; 
				DELETE &b.1;
			run;
			
			proc sql; 
				create table &b.3 as select *, 
				lnwage-MEAN(lnwage) as within_lnwage,
				sum((weight)) as countuny,
				sum(weight*(PUT((sumweight/&SUM),rk.)="1.F0025")) as F0025uny,
				sum(weight*(PUT((sumweight/&SUM),rk.)="2.F2575")) as F2575uny, 
				sum(weight*(PUT((sumweight/&SUM),rk.)="3.F7590")) as F7590uny, 
				sum(weight*(PUT((sumweight/&SUM),rk.)="4.F9099")) as F9099uny, 
				sum(weight*(PUT((sumweight/&SUM),rk.)="5.F9910")) as F9910uny,
				sum(weight*(PUT((sumweight/&SUM),decile.)="d0")) as d0uny,
				sum(weight*(PUT((sumweight/&SUM),decile.)="d1")) as d1uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d2")) as d2uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d3")) as d3uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d4")) as d4uny,
				sum(weight*(PUT((sumweight/&SUM),decile.)="d5")) as d5uny,
				sum(weight*(PUT((sumweight/&SUM),decile.)="d6")) as d6uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d7")) as d7uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d8")) as d8uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d9")) as d9uny,
				sum(weight*(migrant=1)) as migrantuny,
				sum(weight*(nonmigrant=1)) as nonmigrantuny
				from &b.2 (where=( wage NE .)) group by &unit.; 
			quit;

			proc datasets LIBRARY=user; 
				DELETE &b.2;
			run;

			data &b.4; set &b.3;
			rk=sumweight/&SUM;

			F0025=(PUT(rk,rk.)="1.F0025");
			F2575=(PUT(rk,rk.)="2.F2575");
			F7590=(PUT(rk,rk.)="3.F7590");
			F9099=(PUT(rk,rk.)="4.F9099");
			F9910=(PUT(rk,rk.)="5.F9910");

			d0=(PUT(rk,decile.)="d0");
			d1=(PUT(rk,decile.)="d1");
			d2=(PUT(rk,decile.)="d2");
			d3=(PUT(rk,decile.)="d3");
			d4=(PUT(rk,decile.)="d4");
			d5=(PUT(rk,decile.)="d5");
			d6=(PUT(rk,decile.)="d6");
			d7=(PUT(rk,decile.)="d7");
			d8=(PUT(rk,decile.)="d8");
			d9=(PUT(rk,decile.)="d9");

			if countuny>weight then do; 

				xF0025=(F0025uny-F0025*weight)/(countuny-weight);
				xF2575=(F2575uny-F2575*weight)/(countuny-weight);
				xF7590=(F7590uny-F7590*weight)/(countuny-weight);
				xF9099=(F9099uny-F9099*weight)/(countuny-weight);
				xF9910=(F9910uny-F9910*weight)/(countuny-weight);

				xd0=(d0uny-d0*weight)/(countuny-weight);
				xd1=(d1uny-d1*weight)/(countuny-weight);
				xd2=(d2uny-d2*weight)/(countuny-weight);
				xd3=(d3uny-d3*weight)/(countuny-weight);
				xd4=(d4uny-d4*weight)/(countuny-weight);
				xd5=(d5uny-d5*weight)/(countuny-weight);
				xd6=(d6uny-d6*weight)/(countuny-weight);
				xd7=(d7uny-d7*weight)/(countuny-weight);
				xd8=(d8uny-d8*weight)/(countuny-weight);
				xd9=(d9uny-d9*weight)/(countuny-weight);


				xmigrant=(migrantuny-migrant*weight)/(countuny-weight);
				xnonmigrant=(nonmigrantuny-weight+migrant*weight)/(countuny-weight);

			end;


			all=1;
			if countuny>0 then w_unit=1/countuny;
			run;

			proc datasets LIBRARY=user; 
				DELETE &b.3;
			run;

			proc means data= &b.4 ; 
			Title "&year. &unit Fractile Exposure per Fractile &SUM";
			var wage xf9910;
				class all rk ; 
				types all rk ;
			class all rk ; 
			format rk rk.;
			weight weight;
			run;

			proc means data= &b.4 ; 
			Title "&year. &unit Fractile Exposure per Fractile &SUM";
			var wage xd9;
				class all rk ; 
				types all rk ;
			class all rk ; 
			format rk decile.;
			weight weight;
			run;

			proc means data= &b.4 ; 
			Title "&year. &unit Decile Exposure per Fractile &SUM";
			var wage w_unit xd0-xd9;
			class all rk; 
			types all rk; 
			format rk decile. ;
			weight weight;
			where weight>0;
			run;

%mend;

%b_g(2016); %seg2(b2016);

*Migration;
title "Migration segregation";
title2 "Full population";
title3 "";


data mig_reshuffled; set b20164(keep=migrant); 
 	randommig=ranuni(654321);
	rename migrant=migrant_s;
run;

proc sort DATA=mig_reshuffled; 
by randommig; 
run;

data segtest; merge b20164 mig_reshuffled; 
included=1;
run;


	proc datasets LIBRARY=user; 
			DELETE mig_reshuffled;
	RUN;

proc sql; 
create table segtest2 (where=(nbest>1)) as select *, 
	count(est) as nbest,
	sum(migrant) as nbmigrant_u,
	sum(migrant_s) as nbmigrant_s_u,
	sum(1-migrant) as nbnonmigrant_u,
	sum(1-migrant_s) as nbnonmigrant_s_u  
		from segtest	(where=(included=1)) group by est; 
quit;

proc sql; 
create table segtest3 as select *, 
	sum(migrant) as nbmigrant_tot,
	sum(migrant_s) as nbmigrant_s_tot,
	sum(1-migrant) as nbnonmigrant_tot,
	sum(1-migrant_s) as nbnonmigrant_s_tot from segtest2; 
quit;

	proc datasets LIBRARY=user; 
			DELETE segtest2;
	RUN;

data segtest4; set segtest3; 
	xmigrant=(nbmigrant_u-migrant)/(nbest-1);
	xmigrant_s=(nbmigrant_s_u-migrant_s)/(nbest-1);
	Disim_ctr=0.5*abs(nbmigrant_u/nbmigrant_tot-nbnonmigrant_u/nbnonmigrant_tot)/nbest; 
	Disim_ctr_s=0.5*abs(nbmigrant_s_u/nbmigrant_s_tot-nbnonmigrant_s_u/nbnonmigrant_s_tot)/nbest; 
run;

	proc datasets LIBRARY=user; 
			DELETE segtest3;
	RUN;


proc means data=segtest4 N MEAN SUM ; var Disim_ctr Disim_ctr_s; run;

proc means data=segtest4 ; var xmigrant ; class migrant ; types migrant ; run;

proc means data=segtest4 ; var xmigrant_s ; class migrant_s ; types migrant_s ; run;

proc datasets LIBRARY=user; 
			DELETE segtest4;
RUN;


* Small establishments;
title2 "Full population";
title3 "Small establishments <20";
proc sql; 
create table segtest2 (where=(nbest>1 & nbest<20)) as select *, 
	count(est) as nbest,
	sum(migrant) as nbmigrant_u,
	sum(migrant_s) as nbmigrant_s_u,
	sum(1-migrant) as nbnonmigrant_u,
	sum(1-migrant_s) as nbnonmigrant_s_u  
		from segtest	(where=(included=1)) group by est; 
quit;

proc datasets LIBRARY=user; 
			DELETE segtest;
RUN;


proc sql; 
create table segtest3 as select *, 
	sum(migrant) as nbmigrant_tot,
	sum(migrant_s) as nbmigrant_s_tot,
	sum(1-migrant) as nbnonmigrant_tot,
	sum(1-migrant_s) as nbnonmigrant_s_tot from segtest2; 
quit;

proc datasets LIBRARY=user; 
			DELETE segtest2;
RUN;
data segtest4; set segtest3; 
	xmigrant=(nbmigrant_u-migrant)/(nbest-1);
	xmigrant_s=(nbmigrant_s_u-migrant_s)/(nbest-1);
	Disim_ctr=0.5*abs(nbmigrant_u/nbmigrant_tot-nbnonmigrant_u/nbnonmigrant_tot)/nbest; 
	Disim_ctr_s=0.5*abs(nbmigrant_s_u/nbmigrant_s_tot-nbnonmigrant_s_u/nbnonmigrant_s_tot)/nbest; 
run;

proc datasets LIBRARY=user; 
			DELETE segtest3;
RUN;
proc means data=segtest4 N MEAN SUM ; var Disim_ctr Disim_ctr_s; weight weight; run;

proc means data=segtest4 ; var xmigrant ; class migrant ; types migrant ; weight weight; run;
proc means data=segtest4 ; var xmigrant_s ; class migrant_s ; types migrant_s ; weight weight; run;

proc datasets LIBRARY=user; 
			DELETE segtest4;
RUN;



*********************************;
*********************************;
*********************************;

TITLE "SAMPLE ";
TITLE2 "SAMPLE - No weights ";
TITLE3 "";
data b2016_red; set b20164; 
	if countuny>1 and weight>0;
	myselect=(uniform(124421)>0.9)*1;
	orgsize=put(countuny,orgsize.);
	keep weight wage countuny myselect migrant included orgsize est random lnwage nonmigrant;
run;

	
	%let b=b2016_red; 
	proc sql; 
			create table &b.1 as select *, 
			SUM ((included=1)) as orgsize_nb
			from &b group by orgsize;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b;
	RUN;

		proc sql; 
			create table &b.2 as select *, 
			SUM ((included=1)) as incl_total
			from &b.1;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b.1;
	RUN;

		data &b.3; set &b.2; 
			orgsize_pds=orgsize_nb/incl_total;
			if myselect=1;
		run;

	proc datasets LIBRARY=user; 
				DELETE &b.2;
	RUN;

		proc sql; create table &b.4 as select *, 
			SUM ((included=1)) as countuny_sel
			from &b.3 group by est;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b.3;
	RUN;
		proc sql; create table &b.5 as select *, 
			SUM ((included=1)) as orgsize_nb_sel
			from &b.4 (where=(countuny_sel>1)) group by orgsize;
		quit;

		proc datasets LIBRARY=user; 
				DELETE &b.4;
		RUN;


		proc sql; 
			create table &b.6 as select *, 
			SUM ((included=1)) as incl_total_sel
			from &b.5;
		quit;

		proc datasets LIBRARY=user; 
				DELETE &b.5;
		RUN;


data c2016; set b2016_red6 (rename=(countuny=countuny_old)); 
			orgsize_pds_sel=orgsize_nb_sel/incl_total_sel;
			old_weight=weight;
			new_weight=orgsize_pds/orgsize_pds_sel;
			*weight=(old_weight>0)*new_weight;			
			weight=1;			
			alt_weight=1/(1-(9/10)**(countuny_old-1));
			*weight=1/(1-(9/10)**(countuny_old-1));
			run;

proc datasets LIBRARY=user; 
				DELETE &b.6;
RUN;


title "";
title2 "Sample  - no weights"; 
title3 "";
%seg2(c2016);


*MIGRATION SAMPLE;
title "Migration segregation";
title2 "Sample - no weights";
title3 "";

data mig_reshuffled; set c20164(keep=migrant); 
 	randommig=ranuni(654321);
	rename migrant=migrant_s;
run;

proc sort DATA=mig_reshuffled; 
by randommig; 
run;

data segtest; merge c20164 mig_reshuffled; run;


	proc datasets LIBRARY=user; 
			DELETE mig_reshuffled;
	RUN;

proc sql; 
create table segtest2 (where=(nbest>1)) as select *, 
	count(est) as nbest,
	sum(migrant) as nbmigrant_u,
	sum(migrant_s) as nbmigrant_s_u,
	sum(1-migrant) as nbnonmigrant_u,
	sum(1-migrant_s) as nbnonmigrant_s_u  
		from segtest	(where=(included=1)) group by est; 
quit;

proc sql; 
create table segtest3 as select *, 
	sum(migrant) as nbmigrant_tot,
	sum(migrant_s) as nbmigrant_s_tot,
	sum(1-migrant) as nbnonmigrant_tot,
	sum(1-migrant_s) as nbnonmigrant_s_tot from segtest2; 
quit;

	proc datasets LIBRARY=user; 
			DELETE segtest2;
	RUN;

data segtest4; set segtest3; 
	xmigrant=(nbmigrant_u-migrant)/(nbest-1);
	xmigrant_s=(nbmigrant_s_u-migrant_s)/(nbest-1);
	Disim_ctr=0.5*abs(nbmigrant_u/nbmigrant_tot-nbnonmigrant_u/nbnonmigrant_tot)/nbest; 
	Disim_ctr_s=0.5*abs(nbmigrant_s_u/nbmigrant_s_tot-nbnonmigrant_s_u/nbnonmigrant_s_tot)/nbest; 
run;

	proc datasets LIBRARY=user; 
			DELETE segtest3;
	RUN;


proc means data=segtest4 N MEAN SUM ; var Disim_ctr Disim_ctr_s; weight weight; run;

proc means data=segtest4 ; var xmigrant ; class migrant ; types migrant ; weight weight; run;

proc means data=segtest4 ; var xmigrant_s ; class migrant_s ; types migrant_s ; weight weight; run;

proc datasets LIBRARY=user; 
			DELETE segtest4;
RUN;



*********************************;
*********************************;
*********************************;
*********************************;

TITLE "SAMPLE ";
TITLE2 "SAMPLE - Empirical weights ";
TITLE3 "";
data b2016_red; set b20164; 
	if countuny>1 and weight>0;
	myselect=(uniform(124421)>0.9)*1;
	orgsize=put(countuny,orgsize.);
	keep weight wage countuny myselect migrant included orgsize est random lnwage nonmigrant;
run;

	
	%let b=b2016_red; 
	proc sql; 
			create table &b.1 as select *, 
			SUM ((included=1)) as orgsize_nb
			from &b group by orgsize;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b;
	RUN;

		proc sql; 
			create table &b.2 as select *, 
			SUM ((included=1)) as incl_total
			from &b.1;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b.1;
	RUN;

		data &b.3; set &b.2; 
			orgsize_pds=orgsize_nb/incl_total;
			if myselect=1;
		run;

	proc datasets LIBRARY=user; 
				DELETE &b.2;
	RUN;

		proc sql; create table &b.4 as select *, 
			SUM ((included=1)) as countuny_sel
			from &b.3 group by est;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b.3;
	RUN;
		proc sql; create table &b.5 as select *, 
			SUM ((included=1)) as orgsize_nb_sel
			from &b.4 (where=(countuny_sel>1)) group by orgsize;
		quit;

		proc datasets LIBRARY=user; 
				DELETE &b.4;
		RUN;


		proc sql; 
			create table &b.6 as select *, 
			SUM ((included=1)) as incl_total_sel
			from &b.5;
		quit;

		proc datasets LIBRARY=user; 
				DELETE &b.5;
		RUN;


data c2016; set b2016_red6 (rename=(countuny=countuny_old)); 
			orgsize_pds_sel=orgsize_nb_sel/incl_total_sel;
			old_weight=weight;
			new_weight=orgsize_pds/orgsize_pds_sel;
			weight=(old_weight>0)*new_weight;			
			*weight=1;			
			alt_weight=1/(1-(9/10)**(countuny_old-1));
			*weight=1/(1-(9/10)**(countuny_old-1));
			run;

proc datasets LIBRARY=user; 
				DELETE &b.6;
RUN;

title "";
title2 "Sample / Empirical weights"; 
title3 "";
%seg2(c2016);


*MIGRATION SAMPLE;
title "Migration segregation";
title2 "Sample population - Empirical weights";
title3 "";

data mig_reshuffled; set c20164(keep=migrant); 
 	randommig=ranuni(654321);
	rename migrant=migrant_s;
run;

proc sort DATA=mig_reshuffled; 
by randommig; 
run;

data segtest; merge c20164 mig_reshuffled; run;


	proc datasets LIBRARY=user; 
			DELETE mig_reshuffled;
	RUN;

proc sql; 
create table segtest2 (where=(nbest>1)) as select *, 
	count(est) as nbest,
	sum(migrant) as nbmigrant_u,
	sum(migrant_s) as nbmigrant_s_u,
	sum(1-migrant) as nbnonmigrant_u,
	sum(1-migrant_s) as nbnonmigrant_s_u  
		from segtest	(where=(included=1)) group by est; 
quit;

proc sql; 
create table segtest3 as select *, 
	sum(migrant) as nbmigrant_tot,
	sum(migrant_s) as nbmigrant_s_tot,
	sum(1-migrant) as nbnonmigrant_tot,
	sum(1-migrant_s) as nbnonmigrant_s_tot from segtest2; 
quit;

	proc datasets LIBRARY=user; 
			DELETE segtest2;
	RUN;

data segtest4; set segtest3; 
	xmigrant=(nbmigrant_u-migrant)/(nbest-1);
	xmigrant_s=(nbmigrant_s_u-migrant_s)/(nbest-1);
	Disim_ctr=0.5*abs(nbmigrant_u/nbmigrant_tot-nbnonmigrant_u/nbnonmigrant_tot)/nbest; 
	Disim_ctr_s=0.5*abs(nbmigrant_s_u/nbmigrant_s_tot-nbnonmigrant_s_u/nbnonmigrant_s_tot)/nbest; 
run;

	proc datasets LIBRARY=user; 
			DELETE segtest3;
	RUN;


proc means data=segtest4 N MEAN SUM ; var Disim_ctr Disim_ctr_s; weight weight; run;

proc means data=segtest4 ; var xmigrant ; class migrant ; types migrant ; weight weight; run;

proc means data=segtest4 ; var xmigrant_s ; class migrant_s ; types migrant_s ; weight weight; run;

proc datasets LIBRARY=user; 
			DELETE segtest4;
RUN;


*********************************;
*********************************;
*********************************;
*********************************;

TITLE "SAMPLE ";
TITLE2 "SAMPLE - Mathematical weights ";
TITLE3 "";
data b2016_red; set b20164; 
	if countuny>1 and weight>0;
	myselect=(uniform(124421)>0.9)*1;
	orgsize=put(countuny,orgsize.);
	keep weight wage countuny myselect migrant included orgsize est random lnwage nonmigrant;
run;

	
	%let b=b2016_red; 
	proc sql; 
			create table &b.1 as select *, 
			SUM ((included=1)) as orgsize_nb
			from &b group by orgsize;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b;
	RUN;

		proc sql; 
			create table &b.2 as select *, 
			SUM ((included=1)) as incl_total
			from &b.1;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b.1;
	RUN;

		data &b.3; set &b.2; 
			orgsize_pds=orgsize_nb/incl_total;
			if myselect=1;
		run;

	proc datasets LIBRARY=user; 
				DELETE &b.2;
	RUN;

		proc sql; create table &b.4 as select *, 
			SUM ((included=1)) as countuny_sel
			from &b.3 group by est;
		quit;

	proc datasets LIBRARY=user; 
				DELETE &b.3;
	RUN;
		proc sql; create table &b.5 as select *, 
			SUM ((included=1)) as orgsize_nb_sel
			from &b.4 (where=(countuny_sel>1)) group by orgsize;
		quit;

		proc datasets LIBRARY=user; 
				DELETE &b.4;
		RUN;


		proc sql; 
			create table &b.6 as select *, 
			SUM ((included=1)) as incl_total_sel
			from &b.5;
		quit;

		proc datasets LIBRARY=user; 
				DELETE &b.5;
		RUN;


data c2016; set b2016_red6 (rename=(countuny=countuny_old)); 
			orgsize_pds_sel=orgsize_nb_sel/incl_total_sel;
			old_weight=weight;
			new_weight=orgsize_pds/orgsize_pds_sel;
			weight=(old_weight>0)*new_weight;			
			*weight=1;			
			*alt_weight=1/(1-(9/10)**(countuny_old-1));
			weight=1/(1-(9/10)**(countuny_old-1));
			*rename countuny_sel=countuny;
			run;

proc datasets LIBRARY=user; 
				DELETE &b.6;
RUN;


title "";
title2 "Sample / Mathematical  weights"; 
title3 "";
%seg2(c2016);


*MIGRATION SAMPLE;
title "Migration segregation";
title2 "Sample population - Mathematical weights";
title3 "";

data mig_reshuffled; set c20164(keep=migrant); 
 	randommig=ranuni(654321);
	rename migrant=migrant_s;
run;

proc sort DATA=mig_reshuffled; 
by randommig; 
run;

data segtest; merge c20164 mig_reshuffled; run;


	proc datasets LIBRARY=user; 
			DELETE mig_reshuffled;
	RUN;

proc sql; 
create table segtest2 (where=(nbest>1)) as select *, 
	count(est) as nbest,
	sum(migrant) as nbmigrant_u,
	sum(migrant_s) as nbmigrant_s_u,
	sum(1-migrant) as nbnonmigrant_u,
	sum(1-migrant_s) as nbnonmigrant_s_u  
		from segtest	(where=(included=1)) group by est; 
quit;

proc sql; 
create table segtest3 as select *, 
	sum(migrant) as nbmigrant_tot,
	sum(migrant_s) as nbmigrant_s_tot,
	sum(1-migrant) as nbnonmigrant_tot,
	sum(1-migrant_s) as nbnonmigrant_s_tot from segtest2; 
quit;

	proc datasets LIBRARY=user; 
			DELETE segtest2;
	RUN;

data segtest4; set segtest3; 
	xmigrant=(nbmigrant_u-migrant)/(nbest-1);
	xmigrant_s=(nbmigrant_s_u-migrant_s)/(nbest-1);
	Disim_ctr=0.5*abs(nbmigrant_u/nbmigrant_tot-nbnonmigrant_u/nbnonmigrant_tot)/nbest; 
	Disim_ctr_s=0.5*abs(nbmigrant_s_u/nbmigrant_s_tot-nbnonmigrant_s_u/nbnonmigrant_s_tot)/nbest; 
run;

	proc datasets LIBRARY=user; 
			DELETE segtest3;
	RUN;


proc means data=segtest4 N MEAN SUM ; var Disim_ctr Disim_ctr_s; weight weight; run;

proc means data=segtest4 ; var xmigrant ; class migrant ; types migrant ; weight weight; run;

proc means data=segtest4 ; var xmigrant_s ; class migrant_s ; types migrant_s ; weight weight; run;

proc datasets LIBRARY=user; 
			DELETE segtest4 segtest b20164 c20164;
RUN;
